<?php
namespace Home\Controller;
use Common\Controller\BaseController;
class IndexController extends BaseController
{
    protected $userModel = '';

    function _initialize()
    {
//        $_SESSION['userid'] = 1;
//        $_SESSION['openid'] = 'o4IpFuG5qp4vUWCy5SbES1HlkSoc';

        $this->check_login();
    }

    /**
     * 首页
     */
    public function index()
    {
        $this->check_login();
        $uid = $_SESSION['userid'];

        if(IS_POST){
            //将数据插入记录表中
            $data['fk_user_id'] = $_SESSION['userid'];
            $play_time = I('play_time');
            if(!$play_time){
                $this ->rtnResult(0,'提交出错！');
            }
            $data['play_times'] = $play_time;
            $data['create_time'] = date('Y-m-d H:i:s');
            $data['play_num'] = 1;
            $play = M('play')->add($data);
            //同步更新用户表

//            if(!$play){
            !$play && $this ->rtnResult(1,'提交失败！');

            $play = M('user') ->where('pk_id=%d',$_SESSION['userid'])->getField('play_times');
            if(!$play){
                $result = M('user')->where('pk_id=%d',$_SESSION['userid'])->setField('play_times',$play_time);
                $result===false && $this ->rtnResult(1,'保存失败!');
                $this ->rtnResult(0,'提交成功');
            }else{
                if($play>=$play_time){
                    $data['play_times'] = $play_time;
                    $result = M('user') ->where('pk_id=%d',$_SESSION['userid'])->setField('play_times',$play_time);
                    $result===false && $this ->rtnResult(1,'保存失败!');
                }
                $this ->rtnResult(0,'提交成功');
            }
        }else{
            $Weixin = new \Weixin\Controller\WeixinFuwuController();
            $data['wxKey'] = $Weixin->rtnQM();

            $this ->showView($data,'./Public/Spread.html');
        }

        //排名
//        $user = M('user');
//        $play = M('user') ->where('pk_id=%d',$_SESSION['userid'])->getField('play_times');
//        $data['row'] = $user ->field('pk_id,nickname,openid,play_times')->select();
//
//        //击败百分比
//        $person_num = M('user') ->where('play_times > %d',$play)->count(); //比我少的数量
//        $num = M('user')->where('pk_id is not null')->count(); //总数量
//        $data['percent'] = $person_num/$num *100;


    }
//    function monilogin(){
//        $_SESSION['userid'] = 1;
//        $_SESSION['openid'] = 'o4IpFuG5qp4vUWCy5SbES1HlkSoc';
//    }


    /**
     * 获取排行榜
     */
    function rank(){
        //我的时间
        $play = M('user') ->where('pk_id=%d',$_SESSION['userid'])->getField('play_times');

        //排名
        $userModel = M('user');
        $rows = $userModel->where('play_times <> ""')->field('nickname,play_times')->order('play_times')->select();
        $this->data['rows'] = empty($rows) ? array() : $rows;

        //总数据量
        $total = $userModel->count();


        //被我击败的人数
        $person_num = $userModel->where("play_times > $play")->count();

        //击败百分比
        $this->data['percent'] = round($person_num/$total*100,2);
        $this->rtnResult(0,'success');
    }

}